import {defineStore} from 'pinia'
//引入完整版的动态路由
import routes from '@/router/generatorRoutes'

//引入发送请求获取菜单的方法
import * as api from '@/api/caidan.js'
//引入递归routes的方法
import recursionRoutes from '@/utils/recursionRoutes'
//引入动态路由
import dynamicRoutes from '@/router/dynamicRoutes'
//引入静态路由
import staticRoutes from '@/router/staticRoutes'
//引入路由对象
import router from '@/router'
import { useRoute } from 'vue-router'
export const usePermission = defineStore('permission',{
    state(){
        return{
            menulist:[],
            logou:false,//是否退出
            collapse:false//菜单是否收起
        }
    },
    actions:{
        async fetch_menulist(){
            //发起请求，获取菜单
            return api.menuApi()
               .then(res=>{
                // console.log(res)
                // console.log(routes)
                //已经获取用户的路由
                let useRoutes = recursionRoutes(res.data.menuList,routes)
                // console.log(r)
                //给menulist赋值
                this.menulist = useRoutes

                //开始动态添加路由
                //1.先从动态路由中，找到路径式'/'的路由对象
                let target = dynamicRoutes.find(route=>route.path==='/')
                //2.将用户路由变成他的孩子
                target.children = useRoutes
                //3.静态路由也要添加到dynamicRoutes孩子里面
                target.children = [...target.children,...staticRoutes]
                //4.使用addRoute方法，将dynamicRoutes添加到路由的routes配置中
                router.addRoute(target)
               })
        }
    }
})